package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Permission;
import cn.wolfcode.mapper.PermissionMapper;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IPermissionService;
import cn.wolfcode.util.RequiredPermission;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.aop.support.AopUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;

import java.lang.reflect.Method;
import java.util.Collection;
import java.util.List;
import java.util.Map;

@Service
public class PermissionServiceImpl implements IPermissionService{

    @Autowired
    private PermissionMapper permissionMapper;
    @Override
    public void insert(Permission permission) {
        permissionMapper.insert(permission);
    }

    @Override
    public void update(Permission permission) {
        permissionMapper.updateByPrimaryKey(permission);
    }

    @Override
    public void delete(Long id) {
        permissionMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Permission get(Long id) {
       return permissionMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Permission> listAll() {
        return permissionMapper.selectAll();
    }

    @Override
    public PageInfo<Permission> query(QueryObject qo) {

        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Permission> permissions = permissionMapper.queryForList(qo);
        return new PageInfo<>(permissions);
    }

    @Autowired
    private ApplicationContext ctx; //spring上下文对象

    @Override
    public void reload() {
        //需求:把每个控制器中的贴了权限注解的方法转化为权限数据,并插入到数据库
        //查询数据库中已经拥有的权限表达式
        List<String> permissions = permissionMapper.selectAllExpression();
        //1.获取所有的控制器
        Map<String, Object> map = ctx.getBeansWithAnnotation(Controller.class);//获取贴注解的bean
        Collection<Object> controllers = map.values();
        //2.遍历获取每个控制器中的方法
        for (Object controller : controllers) {
            //如果controller是普通的controller类,它的父类是object 其没有贴注解,效果不影响
            //判断是否cglib代理,如果不是,就跳过
            if (AopUtils.isCglibProxy(controller)){
                continue;//执行下一次循环
            }
            //代理对象中的方法上的注解不能被继承,所以获取不到注解
            //控制器的字节码对象(controller是代理对象,获取它的父类,即可拿到注解)
            Class<?> clazz = controller.getClass().getSuperclass();
            //3.遍历获取到每个方法
            Method[] methods = clazz.getDeclaredMethods();
            for (Method method : methods) {
                //4.判断是否有贴自定义的注解 (shiro提供的注解)
                //代理对象中的方法上的在注解不能被继承,所以获取不到注解
                RequiresPermissions annotation = method.getAnnotation(RequiresPermissions.class);
                //判断注解是否为空
                if (annotation != null){
                    //5.从注解中获取权限相关的数据,并封装为权限对象
                    //获取注解中的权限名称name
                    String name = annotation.value()[1];
                    //方式1:获取注解中的权限表达式
                    String expression = annotation.value()[0];
                    //方式2:通过反射拼接出 权限表达式
                   /* String simpleName = clazz.getSimpleName();
                    simpleName = simpleName.replace("Controller","");
                    simpleName = StringUtils.uncapitalize(simpleName);
                    String expression = simpleName + ":" + method.getName();*/

                    if (!permissions.contains(expression)){
                        //6.插入到数据库
                        Permission permission = new Permission();
                        permission.setName(name);
                        permission.setExpression(expression);
                        permissionMapper.insert(permission);
                    }
                }
            }
        }
    }

    @Override
    public List<String> selectPermissionByEmpId(Long id) {
       List<String> list = permissionMapper.selectPermissionByEmpId(id);
        return list;
    }
}
